/* eslint-disable no-undef */
import {
    couplesBG,getHomePageBG,setHomePageBG
} from "../../utils/api/user";
//获取应用实例
const app = getApp();
const dayjs = require("../../utils/dayjs/dayjs.min");
import { wxShowToast } from "../../utils/promisify";
Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        ImgUrl: app.globalData.ImgUrl,
        displayArea: app.globalData.displayArea,
        homePageBG:null, //首页背景
        editRecord:false, // 编辑状态
        dailySchedule: null, // 日课表背景
        weeklySchedule: null, // 周课表背景
        startTime: null, // 情侣课表背景开始时间
        endTime:null, // 课表结束时间
        startTimeStr:"",
        endTimeStr:"",
        previewBG:false, // 预览
        oldData:{
            _homePageBG:"",
            _startTimeStr:"",
            _endTimeStr:"",
        },
    },
    onLoad: function () {
        this.setData({
            homePageBG:"",
            startTimeStr:dayjs().format("YYYY年MM月DD日 HH:mm"),
            endTimeStr:dayjs().add(7,"day").format("YYYY年MM月DD日 HH:mm"),
            startTime: dayjs().unix(), // 情侣课表背景开始时间
            endTime:dayjs().add(7,"day").unix(), // 课表结束时间
        });
        getHomePageBG().then(v=>{
            console.log("v", v);
            let oldData = {
                _homePageBG:v.data.image,
                _startTimeStr:dayjs.unix(v.data.starttime).format("MM月DD日 HH:mm"),
                _endTimeStr:dayjs.unix(v.data.endtime).format("MM月DD日 HH:mm"),
                _startTime: v.data.starttime, // 情侣课表背景开始时间
                _endTime:v.data.endtime, // 课表结束时间
            };
            this.setData({
                oldData
            });
            console.log("object :>> ", this.data);

        });

    },
    /**
     * 后退一页
     */
    BackPage() {
        if (this.data.editRecord) {
            this.setData({
                editRecord:false
            });
            this.oldNewInfoswitch();
        } else {
            wx.navigateBack({
                delta: 1,
            });
        }

    },
    /**
     * 切换展示时间
     */
    showTime(e) {
        let id = e.currentTarget.dataset.id;
        console.log(e.currentTarget.dataset.id);
        let displayTimeList = this.data.displayTimeList.map((v, i) => {
            v.checked = id === i;
            return v;
        });
        this.setData({
            displayTimeList,
        });
    },
    /**
     * 修改背景
     */
    modifyImage(e) {
        let type = e.currentTarget.dataset.type;
        wx.chooseImage({
            count: 1,
            sizeType: ["compressed"],
            sourceType: ["album"],
            success: (res) => {
                // tempFilePath可以作为img标签的src属性显示图片
                const tempFilePaths = res.tempFilePaths;
                switch (type) {
                case "Couples":
                    couplesBG(tempFilePaths[0]).then((v) => {
                        console.log(v);
                        app.getSet();
                        v.code && wxShowToast(v.msg);
                        this.setData({
                            homePageBG:
                                    app.globalData.ImgUrl + v.data.imgurl,
                        });
                    });
                    break;
                default:
                    wxShowToast("设置失败,请重试");
                    break;
                }
            },
        });
    },
    /**
     * 编辑已有的背景
     */
    editRecordBtn(){
        this.oldNewInfoswitch();
        this.setData({
            editRecord:true,
        });
    },
    oldNewInfoswitch(){
        let {homePageBG,startTime,endTime} = this.data;
        let {_homePageBG,_startTime,_endTime} = this.data.oldData;
        this.setData({
            homePageBG:_homePageBG,
            startTimeStr:dayjs.unix(_startTime).format("YYYY年MM月DD日 HH:mm"),
            endTimeStr:dayjs.unix(_endTime).format("YYYY年MM月DD日 HH:mm"),
            startTime:_startTime,
            endTime:_endTime,
            oldData:{
                homePageBG,startTime,endTime
            }
        });
    },
    /**
     * 首页背景开始时间
     */
    TimePicker(e) {
        let day = dayjs(e.detail).format("YYYY年MM月DD日");
        let am = dayjs(e.detail).format("A") == "PM" ? "下午" : "上午";
        let time = dayjs(e.detail).format("HH:mm");
        let startTimeStr = `${day} ${am} ${time}`;
        this.setData({
            startTimeStr,
            startTime: dayjs(e.detail).unix(),
        });
    },
    /**
     * 首页背景结束时间
     */
    ETimePicker(e) {
        let day = dayjs(e.detail).format("YYYY年MM月DD日");
        let am = dayjs(e.detail).format("A") == "PM" ? "下午" : "上午";
        let time = dayjs(e.detail).format("HH:mm");
        let endTimeStr = `${day} ${am} ${time}`;
        this.setData({
            endTimeStr,
            endTime:dayjs(e.detail).unix(),
        });
    },
    /**
     * 预览当前表情
     */
    previewImg(){
        this.setData({
            previewBG:true
        });
        console.log("previewImg :>> ", );
    },
    hideModal(){
        this.setData({
            previewBG:false
        });
    },
    /**
     * 保存背景
     */
    saveCouplesBG() {
        let { endTime, startTime, homePageBG } = this.data;
        console.log("pram :>> ", endTime, startTime, homePageBG);
        let _startTime = startTime ? startTime : dayjs().unix();
        let _endTime =  endTime ? endTime : dayjs().add(7,"day").unix();
        console.log("_endTime :>> ", _startTime,_endTime);
        let pram = {
            image:homePageBG,
            starttime:_startTime,
            endtime:_endTime,
        };
        console.log("pram :>> ", pram);
        setHomePageBG(pram).then(res=>{
            console.log("res", res);
            wx.showToast({
                title: res.msg,
                icon: "none",
                duration: 1500,
                mask: false,
            });
            res.code && app.getSet();
        });
    },
});
